package org.tea.inventory.admin.web.admin;

import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.QuestionDetail;
import org.tea.inventory.db.service.QuestionDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/questionDetail")
public class QuestionDetailController {

    @Autowired
    private QuestionDetailService questionDetailService;

    @GetMapping("list")
    public Object list(@LoginAdmin Integer adminId,
                       @RequestParam(value = "page", defaultValue = "1")Integer page,
                       @RequestParam(value = "limit", defaultValue = "10")Integer limit,
                       QuestionDetail questionDetail) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        Map<String,Object> data = questionDetailService.selectByExample(questionDetail,page,limit);

        return ResponseUtil.ok(data);
    }

    /**
     * 获取指定id的问题
     */
    @GetMapping("read")
    public Object read(@LoginAdmin Integer adminId, Integer id) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(id == null) {
            return  ResponseUtil.badArgumentValue();
        }

        QuestionDetail questionDetail = questionDetailService.selectByPrimaryKey(id);

        return ResponseUtil.ok(questionDetail);
    }

    /**
     * 创建问题
     */
    @PostMapping("create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody QuestionDetail questionDetail) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        questionDetailService.insert(questionDetail);
        QuestionDetail qd = questionDetailService.selectByPrimaryKey(questionDetail.getId());
        return ResponseUtil.ok(qd);
    }

    /**
     * 更新问题信息
     */
    @PostMapping("update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody QuestionDetail questionDetail) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        questionDetailService.updateByPrimaryKeySelective(questionDetail);
        QuestionDetail qd = questionDetailService.selectByPrimaryKey(questionDetail.getId());
        return ResponseUtil.ok(qd);
    }

    /**
     * 删除问题信息
     */
    @PostMapping("delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody QuestionDetail questionDetail) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(questionDetail == null || questionDetail.getId() == null) {
            return ResponseUtil.badArgumentValue();
        }
        questionDetailService.deleteByPrimaryKey(questionDetail.getId());
        return ResponseUtil.ok();
    }
}
